package com.sky.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.admin.DishMapper;
import com.sky.mapper.admin.SetmealDishMapper;
import com.sky.mapper.admin.SetmealMapper;
import com.sky.pojo.Dish;
import com.sky.pojo.Setmeal;
import com.sky.pojo.SetmealDish;
import com.sky.result.PageResult;
import com.sky.service.admin.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description SetmealServicelmpl
 * @Author zhengyuan&junjun
 * @Date 2024-10-04
 */
@Service
@Transactional
public class SetmealServicelmpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;


    @Override
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //向套餐表插入数据
        setmealMapper.insert(setmeal);
        //获取生成套餐的id
        Long setmealid = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //将生成的套餐id放入setmealDishes集合中
        setmealDishes.forEach(s ->{
            s.setSetmealId(setmealid);
        });
        //向套餐与菜品关系表中插入数据
        setmealDishMapper.insert(setmealDishes);

    }

    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {


        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> p = setmealMapper.select(setmealPageQueryDTO);
        return new PageResult(p.getTotal(),p.getResult());
    }

    @Override
    public void DeleteSetmeal(List<Long> ids) {
        //遍历集合获取id
        ids.forEach(id ->{
            Setmeal setmeal = setmealMapper.getById(id);
             //判断当前菜品是否正在启售中，如果是抛出异常
            if  (setmeal.getStatus() == 1){

                throw  new DeletionNotAllowedException("正在启售中的套餐不能删除！！！");


            }




        });
         ids.forEach(setmealId ->{
             //删除套餐表中的数据
             setmealMapper.delete(setmealId);
             //删除套餐菜品关系表中的数据
             setmealDishMapper.delete(setmealId);


         });



    }

    @Override
    public SetmealVO getById(Long id) {

      SetmealVO setmealVO = setmealMapper. getByIdWithDish(id);

        return setmealVO;
    }

    @Override
    public void update(SetmealDTO setmealDTO) {
       Setmeal setmeal = new Setmeal();
       BeanUtils.copyProperties(setmealDTO,setmeal);


        //修改套餐表
        setmealMapper. update(setmeal);
        //获取套餐id
        Long setmealDTOId = setmealDTO.getId();
        //2、删除套餐和菜品的关联关系，操作setmeal_dish表，执行delete
        setmealDishMapper.delete(setmealDTOId);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(s ->{
            s.setSetmealId( setmealDTOId);

        });
        //3、重新插入套餐和菜品的关联关系，操作setmeal_dish表，执行insert
        setmealDishMapper.insert(setmealDishes);


    }

    @Override
    public void startOrStop(Integer status, Long id) {

        //起售套餐时，判断套餐内是否有停售菜品，有停售菜品提示"套餐内包含未启售菜品，无法启售"
        if (status == 1){
           //查询菜品id
          List<Dish> DishId = dishMapper.getBysetmealId(id);

          if (DishId != null && DishId.size() != 0){
                //获取菜品Id
              DishId.forEach(ids->{
                  //判断套餐内是否有停售菜品
                  if (ids.getStatus() == 0  ){

                 throw  new SetmealEnableFailedException("套餐内包含未启售菜品，无法启售");

                  }

              } );
          }
        }
        Setmeal setmeal = Setmeal.builder().id(id).status(status).build();
        setmealMapper.update(setmeal);


    }
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}

